package org.jypj.dev.controller;

import org.jypj.dev.model.ApkInfo;
import org.jypj.dev.service.IApkInfoService;
import org.jypj.dev.util.DateUtils;
import org.jypj.dev.util.FileUtil;
import org.jypj.dev.util.PropertyUtil;
import org.jypj.dev.util.StringUtil;
import org.jypj.dev.vo.LayResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ChenYu on 2017-05-16.
 */
@RequestMapping(value = "apkController")
@Controller
public class ApkController {

    @Resource
    private IApkInfoService apkInfoService;

    /**
     * 跳转到apk列表页
     *
     * @return
     */
    @RequestMapping(value = "toApkList.action")
    public String toApkList() {
        return "back/sys/apk_list";
    }


    /**
     * 查询所有apk信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectAllApkList.action")
    public Map<String, Object> selectAllApkList() {
        Map<String, Object> dataMap = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        int code = 0;
        try {
            String file_server_url = PropertyUtil.getProperties("file_server_url");
            List<ApkInfo> apkInfos = apkInfoService.selectAllByMap(queryMap);
            for (ApkInfo apkInfo : apkInfos) {
                apkInfo.setApkUrl(file_server_url + apkInfo.getApkUrl());
            }
            dataMap.put("data", apkInfos);
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        dataMap.put("code", code);
        return dataMap;
    }

    /**
     * 上传APK文件
     *
     * @param file
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "uploadApk.action")
    public LayResult uploadApk(MultipartFile file) {
        LayResult layResult = new LayResult();
        try {
            String upload = FileUtil.upload("/apk", file);
            layResult.getData().put("src", upload);
            layResult.getData().put("title", file.getOriginalFilename());
        } catch (Exception e) {
            e.printStackTrace();
            layResult.setCode(-1);
        }
        return layResult;
    }

    /**
     * 保存或者更新
     *
     * @return
     */
    @RequestMapping("saveOrUpdate.action")
    @ResponseBody
    public Map<String, Object> saveOrUpdate(ApkInfo apkInfo) {
        Map<String, Object> map = new HashMap<>();
        int code = 0;
        try {
            String todayString = DateUtils.getTodayString("yyyy-MM-dd HH:mm:ss");
            apkInfo.setCreateTime(todayString);
            apkInfoService.saveApkInfo(apkInfo);
        } catch (Exception e) {
            code = -1;
            e.printStackTrace();
        }
        map.put("code", code);
        return map;
    }

    /**
     * 保存或者更新
     *
     * @return
     */
    @RequestMapping("deleteObject.action")
    @ResponseBody
    public Map<String, Object> deleteObject(String id) {
        Map<String, Object> map = new HashMap<>();
        int code = 0;
        try {
            int i = apkInfoService.deleteApkInfoById(id);
        } catch (Exception e) {
            code = -1;
            e.printStackTrace();
        }
        map.put("code", code);
        return map;
    }

}
